# coding: utf-8

from PySide6.QtCore import Qt
from PySide6.QtWidgets import QFrame, QVBoxLayout
from qeventmanager import qevent_manager

from common.server import BananaServer
from components import CustomScrollArea
from .class_card import ClassicHeaderCardWidget
from ..tools import check_result


class HomeInterface(CustomScrollArea):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.view = QFrame()
        self.verticalLayout = QVBoxLayout(self.view)
        self.dayrowsWidget = ClassicHeaderCardWidget('香蕉头条', self.view)
        self.latestrowsWidget = ClassicHeaderCardWidget('最新视频', self.view)
        self.likerowsWidget = ClassicHeaderCardWidget('猜你喜欢', self.view)
        self.a_vodrowsWidget = ClassicHeaderCardWidget('偷拍自拍', self.view)
        self.b_vodrowsWidget = ClassicHeaderCardWidget('成人动漫', self.view)
        self.c_vodrowsWidget = ClassicHeaderCardWidget('经典伦理', self.view)
        self.tagvodrowsWidget = ClassicHeaderCardWidget('不雅视频', self.view)
        self.hotrowsWidget = ClassicHeaderCardWidget('热门视频', self.view)

        self.__initWidgets()
        self.loadData()

    def __initWidgets(self):
        self.setObjectName("HomeInterface")
        self.setWidget(self.view)

        self.verticalLayout.setAlignment(Qt.AlignmentFlag.AlignTop)
        self.verticalLayout.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout.setSpacing(20)
        self.verticalLayout.addWidget(self.dayrowsWidget)
        self.verticalLayout.addWidget(self.latestrowsWidget)
        self.verticalLayout.addWidget(self.likerowsWidget)
        self.verticalLayout.addWidget(self.a_vodrowsWidget)
        self.verticalLayout.addWidget(self.b_vodrowsWidget)
        self.verticalLayout.addWidget(self.c_vodrowsWidget)
        self.verticalLayout.addWidget(self.tagvodrowsWidget)
        self.verticalLayout.addWidget(self.hotrowsWidget)

    def loadData(self):
        qevent_manager.addTask(BananaServer.home, slot=self.loadDataFinished)

    def loadDataFinished(self, result: dict):
        data = check_result(result)
        if not data:
            return

        self.dayrowsWidget.load(data['dayrows'])
        self.latestrowsWidget.load(data['latestrows'])
        self.likerowsWidget.load(data['likerows'])
        self.a_vodrowsWidget.load(data['a_vodrows'])
        self.b_vodrowsWidget.load(data['b_vodrows'])
        self.c_vodrowsWidget.load(data['c_vodrows'])
        self.tagvodrowsWidget.load(data['tagvodrows'])
        self.hotrowsWidget.load(data['hotrows'])
